﻿<?php 
require_once("maincore.php");
if (!defined("THNOJ")) die("Outside of the site");

$cache_time=100; 
//$OJ_CACHE_SHARE=true;
if (isset($_GET['cid'])) $ucid="&cid=".intval($_GET['cid']);
else $ucid="";
require_once("./include/db_info.inc.php");
?>

<?php 
require_once("contest-header.php");
$pr_flag=false;
$co_flag=false;
if (isset($_GET['id'])) 
{
	$sql="SELECT * FROM `problem` WHERE `defunct`='N' AND `problem_id`=".$_GET['id'];
}
else
if (isset($_GET['pid'])){
	// contest
	
	$pid=intval($_GET['pid']);

	if (isset($_GET['cid'])) {
		$cid=intval($_GET['cid']);
		$sql="SELECT langmask,private FROM `contest` WHERE `defunct`='N' AND `contest_id`=$cid AND start_time < NOW()";

		//echo $sql;
		$result=mysql_query($sql);
		$rows_cnt=mysql_num_rows($result);
		$ok_cnt=$rows_cnt==1;	
		$row=mysql_fetch_row($result);
		$langmask=$row[0];
		$congkhai=($row[1]!='1');
		mysql_free_result($result);
		if ($rows_cnt!=1){
			// not started
			echo "Không có kì thi!";
			require_once("oj-footer.php");
			exit(0);
		}else{
			// started
			$sql="SELECT * FROM `problem` WHERE `defunct`='N' AND `problem_id`=(
				SELECT `problem_id` FROM `contest_problem` WHERE `contest_id`=$cid AND `problem_id`=$pid
				)";
		}
	} else {
		$sql="SELECT * FROM `problem` WHERE `defunct`='N' AND `problem_id`=$pid";
	}
	// public
	
	if (!$congkhai) {
		$sql1="select * from privilege where user_id='".$_SESSION['user_id']."' and rightstr='c$cid' limit 1";
		$result1=mysql_query($sql1) or die(mysql_error());
		if (!mysql_num_rows($result1)){
			echo "<br><h1>Không được tham gia!</h1>";
			require_once("oj-footer.php");
			exit(1);
		}
	}
	$co_flag=true;
}else{
	require_once("oj-header.php");
	echo "<title>Không có đề bài</title><h2>Không có bài toán</h2>";
	require_once("oj-footer.php");
	exit(0);
}
//echo $sql;
$result=mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($result)!=1){
   if(isset($_GET['id'])){
      $id=intval($_GET['id']);
	   mysql_free_result($result);
	   $sql="SELECT  contest.`contest_id` , contest.`title`,contest_problem.num FROM `contest_problem`,`contest` WHERE contest.contest_id=contest_problem.contest_id and `problem_id`=$id and defunct='N'  ORDER BY `num`";
	   //echo $sql;
           $result=mysql_query($sql);
	   if($i=mysql_num_rows($result)){
	      echo "This problem is in Contest(s) below:<br>";
		   for (;$i>0;$i--){
				$row=mysql_fetch_row($result);
				echo "<a href=problem.php?cid=$row[0]&pid=$row[2]>Kì thi $row[0]:$row[1]</a><br>";
				
			}
		}else{
			echo "<title>Không có bài toán</title>";
			echo "<h2>Không có bài toán</h2>";
		}
   }else{
		echo "<title>Không có bài toán</title>";
		echo "<h2>Không có bài toán</h2>";
	}
}else{
	$row=mysql_fetch_object($result);
	if ($pr_flag){
		echo "<title>Bài toán $row->problem_id. -- $row->title</title>";
		echo "<center><h2>$id: $row->title</h2>";
	}else{
		$PID="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		echo "<title>Bài toán $pid: $row->title </title>";
		echo "<center><h2>Bài toán $pid: $row->title</h2>";
	}
	echo "<span class=green>Giới hạn thời gian: </span>$row->time_limit giây&nbsp;&nbsp;";
	echo "<span class=green>Giới hạn bộ nhớ: </span>".$row->memory_limit." MB";
	if ($row->spj) echo "giây&nbsp;&nbsp;<span class=red>Special Judge</span>";
	echo "<br><span class=green>Số bài đã nộp: </span>".$row->submit."&nbsp;&nbsp;";
	//echo "<span class=green>Hoàn thành: </span>".$row->accepted."<br>"; 
	
	if ($pr_flag){
		echo "[<a href='submitpage.php?id=$id'>Nộp bài</a>]";
	}else{
		echo "[<a href='submitpage.php?cid=$cid&pid=$pid&langmask=$langmask'>Nộp bài</a>]";
	}
	echo "[<a href='problemstatus.php?id=".$row->problem_id."'>Kết quả</a>]";
	if ($congkhai) echo "[<a href='bbs.php?pid=".$row->problem_id."$ucid'>Trao đổi</a>]";
	
	echo "</center>";
	
	echo "<h2>Mô tả</h2><div class=content>".$row->description."</div>";
	echo "<h2>Input</h2><div class=content>".$row->input."</div>";
	echo "<h2>Output</h2><div class=content>".$row->output."</div>";
	
	$ie6s="";
	$ie6e="";
	if(strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
	{
		$ie6s="<pre>";
		$ie6e="</pre>";
	}
	
	echo "<h2>Ví dụ</h2>
		<h3>Input</h3>
			<div class=content><span class=sampledata>".$ie6s.($row->sample_input).$ie6e."</span></div>";
	echo "<h3>Output</h3>
			<div class=content><span class=sampledata>".$ie6s.($row->sample_output).$ie6e."</span></div>";
	//if ($pr_flag||true) 
	if (strlen($row->hint))
		echo "<h2>Lưu ý</h2>
			<div class=content><p>".nl2br($row->hint)."</p></div>";
	if ($pr_flag) 
		echo "<h2>Mã nguồn</h2>
			<div class=content><p><a href='problemset.php?search=$row->source'>".nl2br($row->source)."</a></p></div>";
	echo "<center>";
	if ($pr_flag){
		echo "[<a href='submitpage.php?id=$id'>Nộp bài</a>]";
	}else{
		echo "[<a href='submitpage.php?cid=$cid&pid=$pid&langmask=$langmask'>Nộp bài</a>]";
	}
	echo "[<a href='problemstatus.php?id=".$row->problem_id."'>Kết quả</a>]";

	if ($congkhai) echo "[<a href='bbs.php?pid=".$row->problem_id."$ucid'>Trao đổi</a>]";
	echo "</center>";
}
mysql_free_result($result);
?>
<?php require("oj-footer.php")?>
